//
//  ZSNewsDataTableView.m
//  fangwangyi
//
//  Created by 臧松 on 16/4/27.
//  Copyright © 2016年 zangsong. All rights reserved.
//

#import "ZSNewsDataTableView.h"
#import "ZSNewsDataTableViewCell.h"
#import "ZSNewsData.h"
#define kWIDTH [UIScreen mainScreen].bounds.size.width
#define kHEIGHT [UIScreen mainScreen].bounds.size.height

@interface ZSNewsDataTableView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSMutableArray * news;

@end
@implementation ZSNewsDataTableView
// 懒加载
-(NSMutableArray *)news{
    if (_news == nil) {
        _news = [NSMutableArray array];
    }
    return _news;
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.delegate = self;
        self.dataSource = self;
        
    }
    return self;
}
// 接受新闻频道数据模型
-(void)setChannel:(ZSChannel *)channel{
    _channel = channel;
    // 清空之前的数据
    self.news = nil;
    [self reloadData];
    // 下载新闻频道数据
   // NSLog(@"%@",channel.tid);
    [self getNewsDataWithTid:channel.tid];
}
// 下载对应的新闻数据
-(void)getNewsDataWithTid:(NSString*)tid{
    
    NSString * urlString = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/headline/%@/0-10.html",tid];
    NSURL * url = [NSURL URLWithString:urlString];
    [[[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //NSLog(@"%@",response);
        if (data) {
            //NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
            NSDictionary * dataDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
            //NSLog(@"%@",dataDict[tid]);
            NSArray * dataArray = dataDict[tid];
            for (NSDictionary* dict in dataArray) {
                ZSNewsData * newsData = [ZSNewsData newsDataWithDict:dict];
                [self.news addObject:newsData];
            }
             // 在主线程刷新 ui
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self reloadData];
            });
            
        }
    }] resume];
}
#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}



#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   // NSLog(@"%lu",(unsigned long)self.news.count);
    return self.news.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellId = @"ZSNewsDataTableViewCell";
    ZSNewsDataTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId ];
    if (cell == nil) {
        cell = [[ZSNewsDataTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    ZSNewsData * newData = self.news[indexPath.row];
    cell.newsData = newData;
    
    return cell;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    self.frame = CGRectMake(0, 0, kWIDTH, kHEIGHT - 55);
}
@end
